<?php

namespace app\librarys\getui;

use app\models\User;
use DictionaryAlertMsg;
use IGeTui;
use IGtAPNPayload;
use IGtNotyPopLoadTemplate;
use IGtSingleMessage;
use IGtTarget;
use IGtTransmissionTemplate;
use RequestException;

require_once(dirname(__FILE__) . '/' . 'IGt.Push.php');
require_once(dirname(__FILE__) . '/' . 'igetui/IGt.AppMessage.php');
require_once(dirname(__FILE__) . '/' . 'igetui/IGt.APNPayload.php');
require_once(dirname(__FILE__) . '/' . 'igetui/template/IGt.BaseTemplate.php');
require_once(dirname(__FILE__) . '/' . 'IGt.Batch.php');
require_once(dirname(__FILE__) . '/' . 'igetui/utils/AppConditions.php');
define('APPKEY', 'uII5U5tO3v7R7Ccn51NaG9');
define('APPID', 'K7wJ0VsSewABiL9CijKmB3');
define('MASTERSECRET', 'HZGNw8fETK9OAEpNzWIm7A');
define('CID', 'cb88b1d7e66dd5fef4813e1e3ef54b4f');
define('DEVICETOKEN', '');
define('Alias', '请输入别名');
define('HOST', 'http://sdk.open.api.igexin.com/apiex.htm');

/**
 * Created by PhpStorm.
 * User: Sky
 * Date: 2017/8/8
 * Time: 下午3:34
 */
class GetuiClient
{
//单推接口案例
    public static function pushMessageToSingle($cid, $content)
    {
        $igt = new IGeTui(HOST, APPKEY, MASTERSECRET);
//        $igt = new IGeTui(NULL, APPKEY, MASTERSECRET, false);

        //消息模版：
        // 1.TransmissionTemplate:透传功能模板
        // 2.LinkTemplate:通知打开链接功能模板
        // 3.NotificationTemplate：通知透传功能模板
        // 4.NotyPopLoadTemplate：通知弹框下载功能模板

//    	$template = IGtNotyPopLoadTemplateDemo();
//    	$template = IGtLinkTemplateDemo();
//    	$template = IGtNotificationTemplateDemo();
        $template = self::IGtTransmissionTemplateDemo($content);

        //个推信息体
        $message = new IGtSingleMessage();

        $message->set_isOffline(true);//是否离线
        $message->set_offlineExpireTime(3600 * 12 * 1000);//离线时间
        $message->set_data($template);//设置推送消息类型
//	$message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
        //接收方
        $target = new IGtTarget();
        $target->set_appId(APPID);
//       $target->set_clientId(CID);
        $target->set_clientId($cid);
//    $target->set_alias(Alias);


        try {
            $rep = $igt->pushMessageToSingle($message, $target);
            var_dump($rep);
            User::log(serialize($rep), 'GetiClient,message is ' . $content . '.cid is ' . $cid);

//            echo("<br><br>");

        } catch (RequestException $e) {
            $requstId = $e->getRequestId();
            $rep = $igt->pushMessageToSingle($message, $target, $requstId);
//            var_dump($rep);
            User::log(serialize($rep), 'ERR GetiClient,message is ' . $message . '.target is ' . $target->clientId . '.requestId is ' . $requstId . '.');
//            echo("<br><br>");
        }

    }

    public static function pushMessageToSingleWithLog($cid, $content, $logInfo)
    {
        $igt = new IGeTui(HOST, APPKEY, MASTERSECRET);
//        $igt = new IGeTui(NULL, APPKEY, MASTERSECRET, false);

        //消息模版：
        // 1.TransmissionTemplate:透传功能模板
        // 2.LinkTemplate:通知打开链接功能模板
        // 3.NotificationTemplate：通知透传功能模板
        // 4.NotyPopLoadTemplate：通知弹框下载功能模板

//    	$template = IGtNotyPopLoadTemplateDemo();
//    	$template = IGtLinkTemplateDemo();
//    	$template = IGtNotificationTemplateDemo();
        $template = self::IGtTransmissionTemplateDemo($content);

        //个推信息体
        $message = new IGtSingleMessage();

        $message->set_isOffline(true);//是否离线
        $message->set_offlineExpireTime(3600 * 12 * 1000);//离线时间
        $message->set_data($template);//设置推送消息类型
//	$message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
        //接收方
        $target = new IGtTarget();
        $target->set_appId(APPID);
//       $target->set_clientId(CID);
        $target->set_clientId($cid);
//    $target->set_alias(Alias);


        try {
            $rep = $igt->pushMessageToSingle($message, $target);
//            var_dump($rep);
            User::log(serialize($rep), 'GetiClient,message is ' . $content . '.cid is ' . $cid . ';  ' . $logInfo);

//            echo("<br><br>");

        } catch (RequestException $e) {
            $requstId = $e->getRequestId();
            $rep = $igt->pushMessageToSingle($message, $target, $requstId);
//            var_dump($rep);
            User::log(serialize($rep), 'ERR GetiClient,message is ' . $message . '.target is ' . $target->clientId . '.requestId is ' . $requstId . '.' . ';  ' . $logInfo);
//            echo("<br><br>");
        }

    }

    function IGtNotyPopLoadTemplateDemo()
    {
        $template = new IGtNotyPopLoadTemplate();
        $template->set_appId(APPID);                      //应用appid
        $template->set_appkey(APPKEY);                    //应用appkey
        //通知栏
        $template->set_notyTitle("个推");                 //通知栏标题
        $template->set_notyContent("个推最新版点击下载"); //通知栏内容
        $template->set_notyIcon("");                      //通知栏logo
        $template->set_isBelled(true);                    //是否响铃
        $template->set_isVibrationed(true);               //是否震动
        $template->set_isCleared(true);                   //通知栏是否可清除
        //弹框
        $template->set_popTitle("弹框标题");              //弹框标题
        $template->set_popContent("弹框内容");            //弹框内容
        $template->set_popImage("");                      //弹框图片
        $template->set_popButton1("下载");                //左键
        $template->set_popButton2("取消");                //右键
        //下载
        $template->set_loadIcon("");                      //弹框图片
        $template->set_loadTitle("地震速报下载");
        $template->set_loadUrl("http://dizhensubao.igexin.com/dl/com.ceic.apk");
        $template->set_isAutoInstall(false);
        $template->set_isActived(true);

        //设置通知定时展示时间，结束时间与开始时间相差需大于6分钟，消息推送后，客户端将在指定时间差内展示消息（误差6分钟）
        $begin = "2015-02-28 15:26:22";
        $end = "2015-02-28 15:31:24";
        $template->set_duration($begin, $end);
        return $template;
    }

    public static function IGtTransmissionTemplateDemo($content)
    {
        $template = new IGtTransmissionTemplate();
        $template->set_appId(APPID);//应用appid
        $template->set_appkey(APPKEY);//应用appkey
        $template->set_transmissionType(1);//透传消息类型
//        $template->set_transmissionContent("测试离线dddccccc");//透传内容
        $template->set_transmissionContent($content);//透传内容
        //$template->set_duration(BEGINTIME,ENDTIME); //设置ANDROID客户端在此时间区间内展示消息
        //APN简单推送
//        $template = new IGtAPNTemplate();
//        $apn = new IGtAPNPayload();
//        $alertmsg=new SimpleAlertMsg();
//        $alertmsg->alertMsg="";
//        $apn->alertMsg=$alertmsg;
////        $apn->badge=2;
////        $apn->sound="";
//        $apn->add_customMsg("payload","payload");
//        $apn->contentAvailable=1;
//        $apn->category="ACTIONABLE";
//        $template->set_apnInfo($apn);
//        $message = new IGtSingleMessage();

        //APN高级推送
        $apn = new IGtAPNPayload();
        $alertmsg = new DictionaryAlertMsg();
        $alertmsg->body = "body";
        $alertmsg->actionLocKey = "ActionLockey";
        $alertmsg->locKey = "LocKey";
        $alertmsg->locArgs = array("locargs");
        $alertmsg->launchImage = "launchimage";
//        IOS8.2 支持
        $alertmsg->title = "Title";
        $alertmsg->titleLocKey = "TitleLocKey";
        $alertmsg->titleLocArgs = array("TitleLocArg");

        $apn->alertMsg = $alertmsg;
        $apn->badge = 7;
        $apn->sound = "";
        $apn->add_customMsg("payload", "payload");
        $apn->contentAvailable = 1;
        $apn->category = "ACTIONABLE";
        $template->set_apnInfo($apn);

        //PushApn老方式传参
//    $template = new IGtAPNTemplate();
//          $template->set_pushInfo("", 10, "", "com.gexin.ios.silence", "", "", "", "");

        return $template;
    }
}